package com.example.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component //很重要，不然aop不会起作用
public class ExampleAop {
	@SuppressWarnings("unused")
	@Pointcut("execution (* com.example.service..*.*(..))||execution(* del*(..))")
	private void anyMethod() {
	}

	@Before("anyMethod() && args(bookName)")
	public void doBefore(String bookName) {

		System.out.println("前置通知:" + bookName);

	}
/*	@Before("anyMethod()")
	public void doBefor(){
		System.out.println("前置通知.....");
	}*/

	@AfterReturning(pointcut = "anyMethod()", returning = "result")
	public void doAfterReturning(String result) {

		System.out.println("后置通知:" + result);

	}

	@After("anyMethod()")
	public void doAfter() {

		System.out.println("最终通知");

	}

	@AfterThrowing(pointcut = "anyMethod()", throwing = "e")
	public void doAfterThrowing(Exception e) {

		System.out.println("例外通知:" + e);

	}

	// 环绕通知可以替代前面的任何通知

	@Around("anyMethod()")
	public Object doAround(ProceedingJoinPoint pjp)

	throws Throwable {

		System.out.println("环绕通知进入方法");

		Object result = pjp.proceed();

		System.out.println("环绕通知退出方法");

		return result;

	}

}
